﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 文字滚动组件
    /// </summary>
    public class TextRoll : MonoBehaviour
    {
        public List<RectTransform> rectList = new List<RectTransform>();

        public RectTransform range;
        public float speed = 10;
        public Direction dir = Direction.Left;
        public Action<RectTransform> onNodeOne = null;

        public void Awake()
        {
            if (range == null)
            {
                range = GetComponent<RectTransform>();
            }

            if (rectList.Count == 0)
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    var rt = transform.GetChild(i) as RectTransform;
                    if (rt != null)
                        rectList.Add(rt);
                }
            }
        }

        public void Update()
        {
            for (int i = 0; i < rectList.Count; i++)
            {
                var rect = rectList[i];
                var pos = rect.anchoredPosition;

                var frameSpeed = speed * Time.deltaTime;
                if (dir == Direction.Left)
                {
                    pos.x -= frameSpeed;
                }
                else if (dir == Direction.Up)
                {
                    pos.y += frameSpeed;
                }
                else if (dir == Direction.Bottom)
                {
                    pos.y -= frameSpeed;
                }
                else if (dir == Direction.Right)
                {
                    pos.x += frameSpeed;
                }

                if (dir == Direction.Left)
                {
                    var rangePos = range.anchoredPosition;
                    float width = range.pivot.x * range.rect.width;
                    var verifyX = rangePos.x - width;
//                Debug.Log("验证坐标点: " + verifyX + "  当前坐标点: " + pos.x);
                    if (pos.x <= verifyX)
                    {
                        //获得最后面的节点
                        float maxX = 0;
                        RectTransform minRect = null;
                        for (int j = 0; j < rectList.Count; j++)
                        {
                            var p = rectList[j].anchoredPosition;
                            if (j == 0)
                            {
                                maxX = rectList[j].anchoredPosition.x;
                                minRect = rectList[j];
                                continue;
                            }

                            if (p.x > maxX)
                            {
                                minRect = rectList[j];
                            }
                        }

                        var lastPos = minRect.anchoredPosition;
                        pos = minRect.anchoredPosition;
                        pos.x += width;
                        float lastWidth = minRect.pivot.x * minRect.rect.width;
                        pos.x += lastWidth;
                        rect.anchoredPosition = pos;

                        if (onNodeOne != null)
                            onNodeOne(rect);
                    }
                    else
                    {
                        rect.anchoredPosition = pos;
                    }
                }
            }
        }

        public enum Direction
        {
            Up,
            Left,
            Right,
            Bottom
        }
    }
}